import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { checkToken, checkRole } from '@/utils/check'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/components/HelloWorld.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/components/Register.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/components/Login.vue')
  },
  {
    path: '/solve',
    name: 'solve',
    component: () => import('@/components/SolveLog.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/edit',
    name: 'edit',
    component: () => import('@/components/AddQus.vue'),
    meta: {
      requireAuth: true,
      roles: 'editor'
    }
  },
  {
    path: '/work',
    name: 'workGround',
    component: () => import('@/components/WorkGround.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/solve/:cate',
    name: 'solveProblem',
    component: () => import('@/components/SolveBk.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/solve/udf',
    name: 'userDefineQus',
    component: () => import('@/components/UserDefineQus.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/message',
    name: 'message',
    component: () => import('@/components/LeaveMessage.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/message/show',
    name: 'ShowMessage',
    component: () => import('@/components/ShowMessage.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/user/modify/:mode',
    name: 'ModifyInfo',
    component: () => import('@/components/ModifyInfo.vue')
  },
  {
    path: '/collect',
    name: 'collection',
    component: () => import('@/components/MyCollection.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/detail/:id',
    name: 'QusDetail',
    component: () => import('@/components/QusDetail.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/back',
    name: 'GoBack',
    component: () => import('@/components/GoBack.vue')
  },
  {
    path: '/review',
    name: 'Review',
    component: () => import('@/components/MyReview.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/praise',
    name: 'PraiseMe',
    component: () => import('@/components/PraiseMe.vue')
  },
  {
    path: '/user/answer/:pid',
    name: 'userAddAnswer',
    component: () => import('@/components/UserAddNote.vue'),
    meta: {
      requireAuth: true
    }
  }
  // {
  //   path: '/pdf',
  //   name: 'QusPdf',
  //   component: () => import('@/components/QusPdf.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes
})

router.beforeEach((to, from, next) => {
  if (to.name === 'edit') {
    const required = to.meta.roles
    if (typeof required === 'string' && checkRole(required)) {
      next()
    } else {
      next({ name: 'login' })
    }
  } else if (to.matched.some(r => r.meta.requireAuth)) {
    const token = checkToken()
    const t2 = to.params.token
    if (token !== '' || (typeof t2 === 'string' && t2.length === 25)) {
      next()
    } else {
      next({ name: 'login' })
    }
  } else {
    next()
  }
})

export default router
